import axios from 'axios'
import config from '@/config'
import store from '@/store'

export function mailboxList() {
    return axios({
        method: 'get',
        url: config.serverURI + config.api.mailbox_list
    })
}

export function chatList(type, mailbox, page, lang) {
    let data = {type}
    if (page) {
        data.kwargs = {page}
        if (mailbox) {
            data.kwargs.mailbox = mailbox
        }

        if (lang && lang !== 'all') {
            if (lang.indexOf('+') !== -1) {
                data.kwargs.has_order = true
                lang = lang.replace("+", "")
            } else if (lang.indexOf('-') !== -1) {
                data.kwargs.has_order = false
                lang = lang.replace("-", "")
            }

            if (lang !== 'all') {
                if (lang.indexOf('zh') !== -1) {
                    data.kwargs.filter = {
                        language__in: ['zh-cn', 'zh-tw', 'zh-hk']
                    }
                } else {
                    data.kwargs.filter = {
                        language: lang
                    }
                }
            }

        }
    } else if (mailbox) {
        data.kwargs = {mailbox}
    }
    return axios({
        method: 'post',
        url: config.serverURI + config.api.chat_list,
        data
    })
}

export function searchChat(keyword, mailbox, page, lang) {
    let data = {type: 'all', kwargs: {search_key: keyword}}
    if (page) {
        data.kwargs.page = page
    }

    if (lang && lang !== 'all') {
        if (lang.indexOf('+') !== -1) {
            data.kwargs.has_order = true
            lang = lang.replace("+", "")
        } else if (lang.indexOf('-') !== -1) {
            data.kwargs.has_order = false
            lang = lang.replace("-", "")
        }

        if (lang !== 'all') {
            if (lang.indexOf('zh') !== -1) {
                data.kwargs.filter = {
                    language__in: ['zh-cn', 'zh-tw', 'zh-hk']
                }
            } else {
                data.kwargs.filter = {
                    language: lang
                }
            }
        }

    }

    if (mailbox) {
        data.kwargs.mailbox = mailbox
    }
    return axios({
        method: 'post',
        url: config.serverURI + config.api.chat_list,
        data
    })
}

export function getMessageList(id, page) {
    let data = {chat_id: id}
    if (page) {
        data.kwargs = {page}
    }

    return axios({
        method: 'post',
        url: config.serverURI + config.api.msg_list,
        data
    })
}

export function sendMessage(data) {
    return axios({
        method: 'post',
        url: config.serverURI + config.api.chat_input,
        data
    })
}

export function seen(id) {
    return axios({
        method: 'post',
        url: config.serverURI + config.api.seen_chat,
        data: {chat_id: id}
    })
}

export function mark(id) {
    return axios({
        method: 'post',
        url: config.serverURI + config.api.replied_chat,
        data: {chat_id: id}
    })
}

export function setChatLabel(chat_id, label) {
    return axios({
        method: 'post',
        url: config.serverURI + config.api.set_chat_label,
        data: {chat_id, label}
    })
}

export function getRelatedInfo(id) {
    return axios({
        method: 'post',
        url: config.serverURI + config.api.chat_related_info,
        data: {chat_id: id}
    })
}

export function translateFromChat(id) {
    return axios({
        method: 'post',
        url: config.serverURI + config.api.translate_from_chat,
        data: {msg_id: id}
    })
}

export function translateMessage(param) {
    return axios({
        method: 'post',
        url: config.serverURI + config.api.translate_message,
        data: param
    })
}